// 平台守卫 - 处理不同平台的页面跳转逻辑
import platformDetector from './platform-detector.js';
import { routeConfig } from './route-config.js';

class PlatformGuard {
  constructor() {
    this.platform = platformDetector.platform;
    this.environment = platformDetector.environment;
    this.platformInfo = platformDetector.getPlatformInfo();
    this.config = platformDetector.getPlatformConfig();
  }

  // 获取默认首页路径
  getDefaultHomePage() {
    return this.config.defaultPage;
  }

  // 获取登录后的首页路径
  getHomePageAfterLogin() {
    return this.config.homePage;
  }

  // 检查页面访问权限
  canAccessPage(pagePath) {
    const token = uni.getStorageSync('token');
    const isAuthenticated = !!token;
    
    return routeConfig.canAccessPage(pagePath, this.platform, isAuthenticated);
  }

  // 处理未授权访问
  handleUnauthorizedAccess(pagePath) {
    const token = uni.getStorageSync('token');
    const isAuthenticated = !!token;
    const redirectPath = routeConfig.getRedirectPath(pagePath, this.platform, isAuthenticated);
    
    if (!redirectPath) return;

    let message = '';
    
    if (this.platform === 'web') {
      if (!isAuthenticated) {
        message = '请先登录';
      } else {
        message = '此页面仅支持小程序端访问';
      }
    } else {
      message = '此页面仅支持Web端访问，请使用浏览器打开';
    }

    uni.showModal({
      title: '访问受限',
      content: message,
      showCancel: false,
      success: () => {
        this.reLaunch(redirectPath);
      }
    });
  }

  // 安全的页面跳转
  navigateTo(url, options = {}) {
    const pagePath = url.split('?')[0];
    
    if (this.canAccessPage(pagePath)) {
      uni.navigateTo({
        url: url,
        ...options
      });
    } else {
      this.handleUnauthorizedAccess(pagePath);
    }
  }

  // 安全的重定向
  redirectTo(url, options = {}) {
    const pagePath = url.split('?')[0];
    
    if (this.canAccessPage(pagePath)) {
      uni.redirectTo({
        url: url,
        ...options
      });
    } else {
      this.handleUnauthorizedAccess(pagePath);
    }
  }

  // 安全的重新启动
  reLaunch(url, options = {}) {
    const pagePath = url.split('?')[0];
    
    if (this.canAccessPage(pagePath)) {
      uni.reLaunch({
        url: url,
        ...options
      });
    } else {
      this.handleUnauthorizedAccess(pagePath);
    }
  }

  // 应用启动检查
  checkAppLaunch() {
    console.log('当前平台:', this.platform);
    
    // 延迟执行，确保页面加载完成
    setTimeout(() => {
      const pages = getCurrentPages();
      const currentPage = pages.length > 0 ? pages[pages.length - 1] : null;
      const currentPath = currentPage ? '/' + currentPage.route : '';
      
      console.log('当前页面路径:', currentPath);
      
      // 获取默认首页路径
      const token = uni.getStorageSync('token');
      const isAuthenticated = !!token;
      const defaultHomePage = routeConfig.getDefaultHomePage(this.platform, isAuthenticated);
      
      // 只有在当前页面不是默认首页时才重定向
      if (currentPath && currentPath !== defaultHomePage) {
        console.log(`重定向到默认首页: ${defaultHomePage}`);
        this.reLaunch(defaultHomePage);
      }
    }, 500);
  }
}

export default new PlatformGuard(); 